package com.xcity.game.arena;

import com.xcity.db.entity.Player;
import com.xcity.game.combat.CombatException;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;

import naga.x.game.time.TimeUpdater;

public class ArenaUtils {

	public static int freeNum = 12; // 每日免费次数
	public static int joinCD = 5 * 60 * 1000; // 每场cd 5分钟
	public static int clearCDCost = 1; // 清除cd消耗钻石
	public static int buyExtraNumBaseCost = 2; // 购买额外次数消耗钻石基数,每多购买一次x2
	
	/**
	 * 获取剩余免费次数
	 * @param player
	 * @return
	 */
	public static int getLeftFreeTimes(Player player) {
		tryReset(player);
		return player.getPool().getIntValue(PlayerPool.PROPERTY_ARENA_FREE_NUM);
	}
	
	/**
	 * 获取剩余已购买次数
	 * @param player
	 * @return
	 */
	public static int getLeftBoughtTimes(Player player) {
		return player.getPool().getIntValue(PlayerPool.PROPERTY_ARENA_BUY_NUM);
	}
	
	/**
	 * 获取剩余可参与次数
	 * @param player
	 * @return
	 */
	public static int getLeftJoinTimes(Player player) {
		return getLeftFreeTimes(player) + getLeftBoughtTimes(player);
	}
	
	/**
	 * 获取参与cd
	 * @param player
	 * @return
	 */
	public static int getJoinCD(Player player) {
		long time = player.getPool().getLongValue(PlayerPool.PROPERTY_ARENA_LAST_JOIN_TIME);
		if (time == 0) {
			return 0;
		}
		time = time + joinCD - TimeUpdater.getInstance().now();
		return (int) (time > 0 ? time : 0);
	}
	
	/**
	 * 获取购买次数消耗钻石
	 * @param player
	 * @return
	 */
	public static int getBuyTimesCostStone(Player player) {
		tryReset(player);
		int n = player.getPool().getIntValue(PlayerPool.PROPERTY_ARENA_TODAY_BUY_NUM);
		if (n == 0) {
			return buyExtraNumBaseCost;
		}
		return (int) (buyExtraNumBaseCost * Math.pow(2, n));
	}
	
	/**
	 * 获取清除竞技场cd消耗钻石
	 * @param player
	 * @return
	 */
	public static int getClearCDCostStone(Player player) {
		return getJoinCD(player) > 0 ? clearCDCost : 0;
	}
	
	public static String clearCD(Player player) {
		int stone = getClearCDCostStone(player);
		if (stone <= 0) {
			return MessageConst.NO_NEED_CLEAR_CD;
		}
		boolean succ = player.costStone(stone, Cause.ARENA_CLEAR_CD);
		if (!succ) {
			return MessageConst.STONE_NOT_ENOUTH;
		}
		player.poolSet(PlayerPool.PROPERTY_ARENA_LAST_JOIN_TIME, 0L);
		return null;
	}
	
	/**
	 * 累计参与次数
	 * @param player
	 */
	public static void joined(Player player) {
		tryReset(player);
		int leftFreeNum = player.getPool().getIntValue(PlayerPool.PROPERTY_ARENA_FREE_NUM);
		if (leftFreeNum > 0) { // 优先扣免费次数
			player.poolSubtract(PlayerPool.PROPERTY_ARENA_FREE_NUM, 1);
		} else {
			player.poolSubtract(PlayerPool.PROPERTY_ARENA_BUY_NUM, 1);
		}
		player.poolSet(PlayerPool.PROPERTY_ARENA_LAST_JOIN_TIME, TimeUpdater.getInstance().now());
	}
	
	public static void tryJoin(Player player) throws CombatException {
		if (getJoinCD(player) > 0) {
			throw new CombatException(MessageConst.COOLING_DOWN);
		}
		if (getLeftJoinTimes(player) <= 0) {
			throw new CombatException(MessageConst.ARENA_LEFT_TIMES_ZERO);
		}
//		int stone = getJoinCostStone(player);
//		if (stone > 0 && !player.costStone(stone, Cause.ARENA)) {
//			throw new CombatException(MessageConst.STONE_NOT_ENOUTH);
//		}
		joined(player);
	}
	
	/**
	 * 购买次数
	 * @param player
	 */
	public static String buyTimes(Player player) {
		int stone = getBuyTimesCostStone(player);
		if (!player.costStone(stone, Cause.ARENA)) {
			return MessageConst.STONE_NOT_ENOUTH;
		}
		player.poolAdd(PlayerPool.PROPERTY_ARENA_TODAY_BUY_NUM, 1);
		player.poolAdd(PlayerPool.PROPERTY_ARENA_BUY_NUM, 1);
		return null;
	}
	
	/** 重置某些数据 */
	private static void tryReset(Player player) {
		int day = player.getPool().getIntValue(PlayerPool.PROPERTY_ARENA_LAST_JOIN_DAY);
		if (day != TimeUpdater.getInstance().today()) {
			player.poolSet(PlayerPool.PROPERTY_ARENA_LAST_JOIN_DAY, TimeUpdater.getInstance().today());
			player.poolSet(PlayerPool.PROPERTY_ARENA_FREE_NUM, freeNum);
			player.poolSet(PlayerPool.PROPERTY_ARENA_TODAY_BUY_NUM, 0);
		}
	}
	
}
